/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @package    FeatherCMS
 * @subpackage Javascript
 * @copyright  Copyright (c) 2006, Julien PORTALIER
 * @link       http://portalier.com/feather
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 *
 * @todo I agree: this is a rather ugly way to do it. There's gotta be a better way!
 */

if (!Form)
	var Form = {};

Form.Help = new Class(
{
	initialize: function(form, options)
	{
		this.inputs = [];
		this.options = Object.extend({className: 'jsHelp'}, options || {});
		this.current = null;
		
		$ES('div.required,div.optional', form).each(function(div)
		{
			var help = $E('span.help', div);
			if (help)
			{
				var input = $ES('input,select,textarea', div)[0];
				
				if (input.getAttribute('type') == 'radio')
					div.addEvent('click', this.show.bindWithEvent(this));
				else
				{
					input.addEvent('focus', this.show.bindWithEvent(this));
					input.addEvent('blur',  this.hide.bind(this));
				}
				
				this.inputs[input.getAttribute('name')] = help.innerHTML;
				help.remove();
			}
		}, this);
	},
	
	show: function(evt)
	{
		// gets related input / help message
		if (!['select', 'input', 'textarea'].test(evt.target.getTag()))
		{
			while (evt.target && evt.target.parentNode && evt.target.getTag && evt.target.getTag() !=  'div')
				evt.target = evt.target.parentNode;
			if ($type(evt.target) == 'element')
				evt.target = $E('input,select,textarea', evt.target);
		}
		if (!evt.target)
			return;
		var name = evt.target.getAttribute('name');
		
		// already existing popup? 
		if (this.jsHelp)
		{
			// is it different from the one to display?
			if (this.current == name)
				return;
			else
			{
				this.jsHelp.remove();
				this.jsHelp = null;
			}
		}
		this.current = name;
		
		// help popup
		this.jsHelp = new DOM('div', {'class': this.options.className},
			new DOM('p').setHTML(this.inputs[name])
		).injectBefore(evt.target).show();
	},
	
	hide: function()
	{
		this.jsHelp.remove();
		this.jsHelp  = null;
		this.current = null;
	}
	
	/*
	initialize: function(form, options)
	{
		this.inputs = [];
		this.options = Object.extend({className: 'jsHelp'}, options || {});
		
		$(form).getElementsBySelector('div.required, div.optional').each(function(div)
		{
			var help = $E('span.help', div);
			if (help)
			{
				var input = div.getElementsBySelector('input,select,textarea')[0];
				input.addEvent('focus', this.show.bind(this));
				input.addEvent('blur',  this.hide.bind(this));
				this.inputs[input.getAttribute('name')] = help.innerHTML;
				help.remove();
			}
		}, this); 
	},
	
	show: function(evt)
	{
		var input   = new Event(evt).target();
		
		this.jsHelp = new Element('div', {'class': this.options.className},
			new Element('p').setHTML(this.inputs[input.getAttribute('name')])
		).setStyles({
			position: 'absolute',
			left: input.getLeft() + input.offsetWidth  + 'px',
			top:  input.getTop()  + 'px'
		});
		
		this.fx = new Fx.Style(this.jsHelp, 'opacity').hide();
		this.jsHelp.injectInside(document.body);
		this.fx.goTo(1);
	},
	
	hide: function(evt)
	{
		var input = new Event(evt).target();
		this.jsHelp.remove.delay(210, this.jsHelp);
		this.fx.goTo(0);
	}
	*/
});